package com.jereh.topnews.demo;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jereh.topnews.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * sd卡读写
 */
public class ExternalActivity extends AppCompatActivity {

    private EditText edit;
    private TextView content;
    private String fileName="abc.txt";
    //sd卡路径
    private File dir=Environment.getExternalStorageDirectory();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_external);
        edit=(EditText)findViewById(R.id.edit);
        content=(TextView)findViewById(R.id.content);
        findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //不能保存空内容
                if(edit.getText()!=null&&!edit.getText().toString().equals("")) {
                    if (isSDExist()) {
                        //进行数据的保存
                        if (saveFile()) {
                            //保存成功把信息打印在textview上
                            content.setText(readFile());
                        } else {
                            Toast.makeText(ExternalActivity.this, "文件写入失败", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        //弹出提示信息
                        Toast.makeText(ExternalActivity.this, "手机sd卡不可用", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(ExternalActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    /**
     * 保存文件到sd卡中
     * @return
     */
    public boolean saveFile(){

        try {
            //创建文件
            File file=new File(dir.getCanonicalPath(),fileName);
            //使用缓冲流进行写入
            BufferedWriter bw=new BufferedWriter(new FileWriter(file));
            bw.write(edit.getText().toString());
            bw.flush();
            bw.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 读取文件操作
     * @return
     */
    public String readFile(){
        try {
            File file=new File(dir.getCanonicalPath(),fileName);
            //使用缓冲流读取内容
            BufferedReader br=new BufferedReader(new FileReader(file));
            StringBuffer sb=new StringBuffer();
            String line="";
            //循环读取每一行的内容
            while ((line=br.readLine())!=null) {
                sb.append(line);
            }
            br.close();
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断手机sd卡是否存在
     * @return
     */
    public boolean isSDExist() {
        //获取手机媒体存储的状态
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            return true;
        }
        return false;
    }
}
